//
//  testUSB2.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/30/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>


int main(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    
    io_service_t		serviceObject;
	IOUSBDeviceInterface	**dev = NULL;
	SInt32			score;
	HRESULT			result;
	CFMutableDictionaryRef	entryProperties = NULL;
    

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return -1; // fail
    }

    /* Now we have a dictionary, get an iterator.*/
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
    {
        return -1;
    }

    /* iterate */
    while ((serviceObject = IOIteratorNext(iter)))
    {
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
        
        
        
        // printf("%s(): device added %d.\n", __func__, (int) serviceObject);
		IORegistryEntryCreateCFProperties(serviceObject, &entryProperties, NULL, 0);

		kr = IOCreatePlugInInterfaceForService(serviceObject,
						       kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
						       &plugInInterface, &score);

		if ((kr != kIOReturnSuccess) || !plugInInterface) {
			printf("%s(): Unable to create a plug-in (%08x)\n", __func__, kr);
			continue;
		}

		// create the device interface
		result = (*plugInInterface)->QueryInterface(plugInInterface,
							    CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
							    (LPVOID *)&dev);

		// don’t need the intermediate plug-in after device interface is created
		(*plugInInterface)->Release(plugInInterface);

		if (result || !dev) {
			printf("%s(): Couldn’t create a device interface (%08x)\n", __func__, (int) result);
			continue;
		}

		NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 0];

		UInt16 vendorID, productID;
		(*dev)->GetDeviceVendor(dev, &vendorID);
		(*dev)->GetDeviceProduct(dev, &productID);
		NSString *name = (NSString *) CFDictionaryGetValue(entryProperties, CFSTR(kUSBProductString));
		if (!name)
			continue;
		
		printf(" *dev = %p\n", *dev);
        
        
        IOObjectRelease(device);
    }

   /* Done, release the iterator */
   IOObjectRelease(iter);
   return 0;
}















/*


        printf("%s(): device added %d.\n", __func__, (int) serviceObject);
		IORegistryEntryCreateCFProperties(serviceObject, &entryProperties, NULL, 0);

		kr = IOCreatePlugInInterfaceForService(serviceObject,
						       kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
						       &plugInInterface, &score);

		if ((kr != kIOReturnSuccess) || !plugInInterface) {
			printf("%s(): Unable to create a plug-in (%08x)\n", __func__, kr);
			continue;
		}

		// create the device interface
		result = (*plugInInterface)->QueryInterface(plugInInterface,
							    CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
							    (LPVOID *)&dev);

		// don’t need the intermediate plug-in after device interface is created
		(*plugInInterface)->Release(plugInInterface);

		if (result || !dev) {
			printf("%s(): Couldn’t create a device interface (%08x)\n", __func__, (int) result);
			continue;
		}

		NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 0];

		UInt16 vendorID, productID;
		(*dev)->GetDeviceVendor(dev, &vendorID);
		(*dev)->GetDeviceProduct(dev, &productID);
		NSString *name = (NSString *) CFDictionaryGetValue(entryProperties, CFSTR(kUSBProductString));
		if (!name)
			continue;
		
		printf(" *dev = %p\n", *dev);

		[dict setObject: [NSString stringWithFormat: @"0x%04x", vendorID]
			 forKey: @"VID"];
		[dict setObject: [NSString stringWithFormat: @"0x%04x", productID]
			 forKey: @"PID"];
		[dict setObject: [NSString stringWithString: name]
			 forKey: @"name"];
		[dict setObject: [NSValue valueWithPointer: dev]
			 forKey: @"dev"];
		[dict setObject: [NSNumber numberWithInt: serviceObject]
			 forKey: @"service"];

		[deviceArray addObject: dict];
        
        
*/
        
 